<?php
class PlanesController extends AppController {

	var $name = 'Planes';
	var $uses = array('Plan','Money');
	
    function beforeFilter () {
        parent::beforeFilter();
    }

	function index() {
        $this->set('title_nav_bar','Planes');
		$this->set('planes', $this->paginate());
	}


    function edit($id=0) {

		if ( isset($_POST['save']) ) {
			
			if ($this->Plan->save($this->data['Plan'])) {
				$this->Session->setFlash(__('Guardado correctamente', true));
				$this -> redirect(array('action'=>'index'));
				exit();                
			} else {
				$this->Session->setFlash(__('No se pudo guardar el formulario.', true));
			}  
			
		} 
            
        if ($id == 0) {
                $this->set('title_nav_bar','Crear un Formulario');
                // Nuevo Plan
                $T = $this->Plan->_schema;
                foreach($T as $clave => $valor) { $this->data['Plan'][$clave] = $valor['default']; }
                $this->data['Plan']['id'] = 0;    
        } else {
                $this->set('title_nav_bar','Editar Plan (id:'.$id.')');
				$this->Plan->recursive = 3;
                $this->data= $this->Plan->read(null, $id);
				
        }
		
		$monedas = $this->Money->find('list', array('fields' => array('Money.name')) );
		$this->set('monedas',$monedas);
		
        
        
    }


	function delete() {

		if (empty($this->data['Delete'])) {
			$this->Session->setFlash(__('Debe seleccionar un plan', true));
			$this->redirect(array('action'=>'index'));
		} else {
 
            foreach($this->data['Delete'] as $key=>$value) {
                if ($value==1) { 
                    $this->Plan->delete($key,true); 
                }
            }
            $this->Session->setFlash(__('Plan/es eliminado/s', true));
    		$this->redirect(array('action'=>'index'));

		} 
	}

	
	function eliminarcampo($formulario_id=0,$campo_id=0 ) {

		if ($campo_id==0 OR $formulario_id==0) {
			$this->Session->setFlash(__('Debe seleccionar un campo del formulario', true));
			$this->redirect(array('action'=>'index'));
		} else {
 
            $this->Formularioscampo->delete($campo_id,true); 

            $this->Session->setFlash(__('Campo del Formulario eliminado', true));
    		$this->redirect(array('action'=>'edit/'.$formulario_id));

		} 
	}
	
	
}
